package com.maple.common.utils.agreements;
import com.maple.common.utils.Tools;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class B13AgreementUtil {
    /**
     * 截取字符串
     */
    public static void testRegexp(String s,CallBackWeight callBackWeight) {

        if (s != null && !"".equals(s)) {
            String regex = "82[0-9a-fA-F]{30}8D";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(s);
            while (matcher.find()) {
                String matcherString = matcher.group();
                System.out.println(matcherString);

                byte[] data = parseHexStr2Byte(matcherString);
                if (data.length == 17&&data[0]==0x02&&data[16]==0x0d) {
                    int dian = (data[1] & 7) - 2;
                    /**状态字B校验**/
                    boolean isJingzhong = (data[2] & 1) != 0;//是否是净重
                    boolean isFu = (data[2] & 2) != 0;//单位是否为负
                    boolean isChaozai = (data[2] & 4) != 0;//是否超载
                    boolean isKG = (data[2] & 16) != 0;//单位
                    System.out.println("净重：" + isJingzhong + ",负：" + isFu + ",超载：" + isChaozai + ",kg：" + isKG);
                    if (!isJingzhong) {
                        /**获取重量*/
                        byte[] data1 = Arrays.copyOfRange(data, 4, 10);//毛重
                        getData(dian, data1,callBackWeight);
                    }else {
                        /**获取重量*/
                        byte[] data2 = Arrays.copyOfRange(data, 10, 16);//净重
                        getData(dian, data2,callBackWeight);
                    }
                }

            }
        }



    }

    public static void getData(int dian, byte[] data,CallBackWeight callBackWeight) {

        if (data.length != 6) {
            return;
        }
        StringBuilder weight = new StringBuilder();
        for (int i = 0; i < data.length; i++) {
            weight.append((char) data[i]);
        }
        if (dian > 0) {
            weight.insert(weight.length() - dian, ".");
        }


        try {
            BigDecimal decimal = new BigDecimal(weight.toString().trim());
//            System.out.println(decimal.toString());
            callBackWeight.succeed(decimal.toString());
        } catch (NumberFormatException e) {
            System.out.println("数据解析错误");
        }
    }


    /**
     * 将十六进制转换为二进制
     * <p>
     * 所有数据要进行数据比较，大于0X80 的数据要进行减去0X80操作
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        }
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            if (high >= 8) {
                high = high - 8;
            }
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }
}
